/*
	istack.v
	Stack used by program counter
	
	This project and file(s) are released under GNU GPL v3.
	Please find license file from root directory.
	Meng Sun (c) 2013 <leon.meng.sun@gmail.com>
*/

module istack (Di, CLK, nRESET, EN, MODE, Do);

input [19:0] Di;
input CLK, nRESET, EN, MODE;
output [19:0] Do;

wire [19:0] Di;
wire CLK, nRESET, EN, MODE;
reg [19:0] Do;

reg [19:0] regStack [31:0];
reg [4:0] regStackPointer;

always @(posedge CLK or negedge nRESET)
begin
	if (nRESET == 0) /*Async-reset*/
	begin
		regStackPointer = 5'b0;
	end else if (EN == 1) /*Push or Pop address to or from stack*/
	begin
		case (MODE)
		1'b0: /*Push*/
		begin
			regStack[regStackPointer] = Di;
			regStackPointer = regStackPointer + 5'b1;
			Do = 20'bx;
		end
		1'b1: /*Pop*/
		begin
			regStackPointer = regStackPointer - 5'b1;
			Do = regStack[regStackPointer];
		end
		endcase
	end
end

endmodule